package com.socialsoftware.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.socialsoftware.model.FriendRequest;
import com.socialsoftware.repository.FriendRequestRepository;

import java.util.ArrayList;
import java.util.List;

/**
 * 好友请求ViewModel
 * 用于处理好友请求相关的业务逻辑
 */
public class FriendRequestViewModel extends AndroidViewModel {
    
    private final FriendRequestRepository repository;
    private final LiveData<List<FriendRequest>> friendRequests;
    private final LiveData<Integer> pendingRequestsCount;
    
    public FriendRequestViewModel(@NonNull Application application) {
        super(application);
        repository = new FriendRequestRepository(application);
        
        // 获取数据，如果返回null则使用默认空值
        LiveData<List<FriendRequest>> repoRequests = repository.getPendingRequests();
        friendRequests = repoRequests != null ? repoRequests : getEmptyFriendRequestList();
        
        LiveData<Integer> repoCount = repository.getPendingRequestsCount();
        pendingRequestsCount = repoCount != null ? repoCount : getZeroCount();
    }
    
    /**
     * 获取好友请求列表
     */
    public LiveData<List<FriendRequest>> getFriendRequests() {
        return friendRequests;
    }
    
    /**
     * 获取待处理好友请求数量
     */
    public LiveData<Integer> getPendingRequestsCount() {
        return pendingRequestsCount;
    }
    
    /**
     * 刷新好友请求数据
     */
    public void refreshFriendRequests() {
        repository.fetchPendingRequestsFromServer();
    }
    
    /**
     * 返回空的好友请求列表LiveData
     */
    private LiveData<List<FriendRequest>> getEmptyFriendRequestList() {
        MutableLiveData<List<FriendRequest>> emptyList = new MutableLiveData<>();
        emptyList.setValue(new ArrayList<>());
        return emptyList;
    }
    
    /**
     * 返回值为0的计数LiveData
     */
    private LiveData<Integer> getZeroCount() {
        MutableLiveData<Integer> zeroCount = new MutableLiveData<>();
        zeroCount.setValue(0);
        return zeroCount;
    }
} 